package com.xgr.wonderful.adapter;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xgr.wonderful.MyApplication;
import com.xgr.wonderful.R;
import com.xgr.wonderful.entity.User;
import com.xgr.wonderful.utils.LocalUserUtils;
import com.xgr.wonderful.view.CircleImageView;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NavigationAdapter  extends BaseAdapter{
	
	private String TAG ="NavigationAdapter";
	
	private final int TYPE_COUNT = 2;
	private final int FIRST_TYPE = 0;
	private final int OTHER_TYPE = 1;
	private int currentType;
	
	private LayoutInflater mLayoutInflater;
	private String[] listData;
	private Integer [] mDrawerIds ;
	private Context context;
	
	private User mUser ;
	
	public NavigationAdapter(Context context, String [] listData, 
			Integer [] mDrawerIds ){
		mLayoutInflater = LayoutInflater.from(context) ;
		this.listData = listData ;
		this.context = context ;
		this.mDrawerIds = mDrawerIds ;
		mUser = LocalUserUtils.getInstance().getLocalUser() ;	
	}
	
	private class FirstItemViewHolder {
		CircleImageView mImageView;
		TextView mTextView;
	}

	private class OtherItemViewHolder {
		TextView mTextView;
	}

	
	@Override
	public int getCount() {
		//这里的listData包括了第一个，我 信息，只是没有显示出来 
		return listData.length;
	}

	@Override
	public Object getItem(int position) {
		if (listData != null){
			return listData[position] ;
		}else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return FIRST_TYPE;// 第一种视图
		} else {
			return OTHER_TYPE;
		}
	}
	@Override
	public int getViewTypeCount() {
		return TYPE_COUNT;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View firstItemView = null ;
		View otherItemView = null ;
		
		currentType = getItemViewType(position) ;
		
		switch (currentType) {
		case FIRST_TYPE:
			firstItemView = convertView ;
			FirstItemViewHolder firstItemViewHolder = null ;
			
			if (firstItemView == null){
				firstItemView = mLayoutInflater.inflate(R.layout.item_navi_header, null) ;
				firstItemViewHolder = new FirstItemViewHolder() ;
				firstItemViewHolder.mImageView = (CircleImageView)firstItemView.
						findViewById(R.id.navi_avor) ;
				firstItemViewHolder.mTextView = (TextView)firstItemView.
						findViewById(R.id.navi_username) ;
				firstItemView.setTag(firstItemViewHolder) ;
			}else {
				firstItemViewHolder = (FirstItemViewHolder)firstItemView.getTag() ;
			}
			if (mUser != null){
				firstItemViewHolder.mTextView.setText(mUser.getUsername()) ;
				ImageLoader.getInstance().displayImage(
						mUser.getPhoto()!= null && mUser.getPhoto().getSmall()!= null ?
								mUser.getPhoto().getSmall() : null, 
						firstItemViewHolder.mImageView,
						MyApplication.getInstance().getOptions(R.drawable.user_icon_default)) ;
				
			}
			convertView = firstItemView ;
			break;
		case OTHER_TYPE:
			otherItemView = convertView ;
			OtherItemViewHolder otherItemViewHolder = null ;
			if (otherItemView == null){
				otherItemView = mLayoutInflater.inflate(R.layout.item_navi, null) ;
				
				otherItemViewHolder = new OtherItemViewHolder() ;
				otherItemViewHolder.mTextView = (TextView)otherItemView
						.findViewById(R.id.navi_other_text) ;
				otherItemView.setTag(otherItemViewHolder) ;
			}else {
				otherItemViewHolder = (OtherItemViewHolder)otherItemView.getTag() ;
			}
			otherItemViewHolder.mTextView.setText((CharSequence) getItem(position)) ;
			//动态设置图片
			Drawable drawable = context.getResources().getDrawable(mDrawerIds[position-1]);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			otherItemViewHolder.mTextView.setCompoundDrawables(drawable, null, null, null);
			convertView = otherItemView;
			break ;
		default:
			break;
		}
		
		return convertView;
	}

}
